package com.example.provider.service.Impl;

import java.util.List;

import com.example.common.model.User;
import com.example.common.model.condition.UserCondition;
import com.example.provider.annotation.MyRedisCache;
import com.example.provider.mapper.UserMapper;
import com.example.provider.service.UserService;
import com.example.provider.type.DataType;
import com.example.provider.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 用户信息表Service接口实现
 *
 * @author 510
 * @version 1.0.0 2021-04-23
 */
@Service
@Transactional(readOnly = true)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 邮件发送
     */
    @Async("asyncServer")
    public void sendSimpleMail(String to, String title, String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(title);
        message.setText(content);
        message.setTo(to);
        message.setFrom(from);
        mailSender.send(message);
        logger.info("发送成功");
    }

    /**
     * 检验验证码
     */
    public Boolean checkCode(String userId, String verificode){
        Object o = redisUtil.get(DataType.CHECKCODE, userId);
        String code = (String) o;
        System.out.println("object"+o);
        return o != null && code.equals(verificode);
    }

    @Override
    public IPage<User> findUserByCondition(UserCondition condition) {
        IPage<User> page = condition.buildPage();
        QueryWrapper<User> queryWrapper = condition.buildQueryWrapper(User.class);
        return this.page(page, queryWrapper);
    }

    @Override
    public User getUserById(String userId) {
        return this.getById(userId);
    }

    @Transactional
    @Override
    public boolean addUser(User user) {
        return this.save(user);
    }

    @Transactional
    @Override
    public boolean updateUser(User user) {
        return this.updateById(user);
    }

    @Transactional
    @Override
    public boolean deleteUserById(String userId) {
        return this.removeById(userId);
    }

    @Transactional
    @Override
    public boolean deleteUserByIds(List<String> idList) {
        return this.removeByIds(idList);
    }
}